# 方法一 暴力解法
class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        n = len(nums)
        for i in range(n):
            left = right = 0
            for l in range(0, i):
                left += nums[l]

            for j in range(i + 1, n):
                right += nums[j]
            if left == right:
                return i 
        return -1


# 优化
class Solution:
    def pivotIndex(self, nums: List[int]) -> int:
        n = len(nums)
        left = right = 0
        # 只有一个数
        if n == 1:
            return 0
        # 右边初值
        for j in range(1, n):
            right += nums[j]

        # 循环找到平衡点
        for i in range(1, n):
            if left == right:
                return i - 1 
            else:
                left += nums[i -1]
                right -= nums[i]
        return -1 if left != right else n - 1